﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;

namespace Com.CMS263.DAO
{
    public class StaffDAO
    {
        public StaffDAO() { }

        public bool Add(Staff cert)
        {
            try
            {
                DBHelper.TransSaveOrUpdate(cert);
                return true;
            }
            catch 
            {
                return false;
            }
        }
        public bool Modify(Staff cert)
        {
            try
            {
                DBHelper.TransUpdate(cert);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteById(int id)
        {
            try
            {
                ISession session = DBHelper.GetISession;
                session.CreateQuery("delete from Staff where id="+id);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public IList<Staff> GetAllStaff(int top)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Staff ").SetFirstResult(0).SetMaxResults(top).List<Staff>();
            }
            catch 
            {

                return DBHelper.ExecuteHQL("from Staff ").List<Staff>();
            }
           
        }
        public Staff GetStaffBy(string name, string sid, int dep)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Staff as sta where sta.name = '" + name + "' and sid='" + sid + "' and cate=" + dep).List<Staff>().First();
            }
            catch(Exception ex)
            {

                return null;
            }
            
        }

    }
}
